import {Injectable} from '@angular/core';
import {Headers, RequestOptions} from '@angular/http';
import {adSystemApiUrl, mock} from '../../../config/api';
import {HttpService} from '../../lib/service/http.service';

@Injectable()

export class GroupService {

  constructor(private httpService: HttpService) {

  }

  // 活动管理列表
  getActivityList(page,
                  pageSize,
                  stage,
                  type,
                  name,
                  order_by,
                  status) {
    const url = `${adSystemApiUrl}/v3/member/activity/list?page=${page}&pageSize=${pageSize}&stage=${stage}&type=${type}&name=${name || ''}&order_by=${order_by}&status=${status || ''}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  getGoodsDetail(activity_id, activity_goods_id) {
    const url  = `${adSystemApiUrl}/v3/member/activity_goods/get_goods?activity_id=${activity_id}&activity_goods_id=${activity_goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }
  getGroupDetail(activity_id, activity_goods_id) {
    const url  = `${adSystemApiUrl}/v3/member/activity_report/report?activity_id=${activity_id}&activity_goods_id=${activity_goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }
  // 获取一分拼团列表
  getPennyList(page, pageSize, status) {
    let stage;
    if (status === '未开始') {
      stage = 1;
    } else if (status === '进行中') {
      stage = 2;
    } else {
      stage = 3;
    }
    const url = `${adSystemApiUrl}/v2/member/activity/penny_list?page=${page}&pageSize=${pageSize}&stage=${stage}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取店铺商品类型列表
  getGroupList() {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category/list`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取商品列表
  getGoodsList(page: number,
               pageSize: number,
               applet_goods_category_id?: number,
               keyword?: string,
               status?: string
               ) {
    const _applet_goods_category_id = applet_goods_category_id || '',
      _keyword = keyword || '';
    const url = `${adSystemApiUrl}/v1/member/goods/list` +
      `?applet_goods_category_id=${_applet_goods_category_id}` +
      `&keyword=${_keyword}&page=${page}&pageSize=${pageSize}&status=${status}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取商品绑定了哪些分类
  getbindListAll(goods_id) {
    const url = `${adSystemApiUrl}/v1/member/applet_goods_category_relation/bindListAll?goods_id=${goods_id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 删除活动
  delActivity(id) {
    const url = `${adSystemApiUrl}/v2/member/activity/del`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({id: id});
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 修改活动状态
  postActivityStatus(id, status) {
    const url = `${adSystemApiUrl}/v2/member/activity/status`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({id: id, status: status});
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  // 活动详情
  getActivityInfo(id) {
    const url = `${adSystemApiUrl}/v3/member/activity/info?id=${id}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 获取指定商品sku
  getGoodsSkuList(id,pageSize?) {
    const url = `${adSystemApiUrl}/v1/member/goods_sku/list?goods_id=${id}&pageSize=${pageSize}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  // 新建拼团
  postAddGroup(data) {
    const url = `${adSystemApiUrl}/v3/member/activity/add`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url,body,options).map(rs => rs);
  }

  //修改拼团
  postEditGroup(data) {
    const url = `${adSystemApiUrl}/v3/member/activity/edit`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  //获取中奖详情
  getWinnerPrizeInfo(activityId) {
    const url = `${adSystemApiUrl}/v3/member/activity_goods_prize_winner/get_info_by_activity_id?activity_id=${activityId}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }

  //生成二维码
  addQrcode(data) {
    const url = `${adSystemApiUrl}/v3/member/activity/head_free_add`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify(data);
    return this.httpService.httpPostAuth(url, body, options).map(rs => rs);
  }

  //二维码列表
  getQrcodeList(activityId) {
    const url = `${adSystemApiUrl}/v3/member/activity/activity_qr_list?activity_id=${activityId}&pageSize=100&order_by={"id":"desc"}`;
    return this.httpService.httpGetAuth(url).map(rs => rs);
  }
}
